import localForage from "localforage";
import ajaxiView from "./ajaxiView";

const apiConfigKey = 'apiConfigKey';

export default {
  getItem() {
    return localForage.getItem(apiConfigKey).then(function(value) {
      if(value === null) {
        return ajaxiView.get('/backstage/Config/index').then(function(data) {
          if(data !== false) {
            localForage.setItem(apiConfigKey, data.config).then(function(value) {
              return value;
            }).catch(function(error) {
              console.log(error);
            });

            return data.config;
          }

          return null;
        });
      }

      return value;
    }).catch(function(error) {
      console.log(error);
    });
  },
  removeItem() {
    localForage.removeItem(apiConfigKey).then(function() {
      console.log(apiConfigKey + ' is remove');
    }).catch(function(err) {
      console.log(err);
    });
  }
}